<?php

declare( strict_types = 1 );

namespace App\Common\Model\Base\DataUnit\JoinDy;

use App\Common as com;
use App\Common\Model as nameModel;
use App\Common\Model\Base as nameBase;
use App\Common\Model\Base\DataUnit as nameDataUnit;

/**
 */
class Item extends nameDataUnit\Base {

	use com\Cla\ClaBase\Traits\GoPool;

	/**
	 * 示例：''
	 * @var string
	 */
	private string $_joinConfig_EchoSite;

	/**
	 * @var array
	 */
	private array $_joinConfig_FieldOfKey = [];

	/**
	 * @var array|null[]
	 */
	private array $_option = [
		'isCleanIncompleteData' => null,
	];

	/**
	 * @param nameDataUnit $dataUnit
	 */
	public function __construct( nameBase\DataUnit $dataUnit ) {
		$this->_joinConfig_EchoSite = 'joinDy' . com\Lib\Fun\Class_::getClassBasename( static::class );
		//
		parent::__construct( $dataUnit );
	}

	/**
	 * @return com\Lib\Db\Join
	 * @throws
	 */
	public function goJoin():com\Lib\Db\Join {
		return $this->goPool( 'goJoin', function () {
			//
			$dbJoin = new com\Lib\Db\Join( null );
			//
			$dbJoin->setEchoSite( $this->getEchoSite() );
			//
			if( $this->getIsCleanIncompleteData() !== null ){
				$dbJoin->isDeleteSourceOfJoinEmpty( $this->getIsCleanIncompleteData() );
			}
			return $dbJoin;
		} );
	}

	/**
	 * [事件]
	 * @return void
	 */
	public function eventDataBefore():void {
		$this->_eventDataBefore();
	}

	/**
	 * @param array &$dataQuote 源数据
	 * @return void
	 */
	public function eventDataLater( array &$dataQuote ):void {
		$this->_eventDataLater( $dataQuote );
		//
		if( $this->getIsCleanIncompleteData() ){
			$this->cleanIncompleteData( $dataQuote );
		}
	}

	/**
	 * 清理空数据
	 * @param array $dataQuote
	 */
	private function cleanIncompleteData( array &$dataQuote ):void {
		$echoSite = $this->getEchoSite();
		foreach( $dataQuote as $dataKey => $dataValue ){
			if( is_null( $dataValue[ $echoSite ] ?? null ) ){
				unset( $dataQuote[ $dataKey ] );
			}
		}
	}

	/**
	 * 获取的字段
	 * @return array
	 */
	public function getField():array {
		return array_flip( $this->_joinConfig_FieldOfKey );
	}

	/**
	 * 检查指定的字段是否存在
	 * @param string|null $name 需要检查的字段。[null]field是否有数据设置 [{string}]检查指定字段
	 * @return bool
	 */
	public function isField( string|null $name = null ):bool {
		if( $name === null ){
			return ! empty( $this->_joinConfig_FieldOfKey );
		} else{
			return isset( $this->_joinConfig_FieldOfKey[ $name ] );
		}

	}

	/**
	 * 设置要获取的字段
	 * @param array $field
	 * @return $this
	 */
	public function field( array $field ):self {
		$this->_joinConfig_FieldOfKey = array_flip( $field );
		//
		return $this;
	}

	/**
	 * 获取echo_site
	 * @return string
	 */
	public function getEchoSite():string {
		return $this->_joinConfig_EchoSite;
	}

	/**
	 * echo_site
	 * @param string $echoSite 链接数据保存路径（默认保存在"joinDy{A}"）
	 * @return $this
	 */
	public function setEchoSite( string $echoSite ):self {
		$this->_joinConfig_EchoSite = $echoSite;
		//
		return $this;
	}

	/**
	 * 设置数据到echo_site位置
	 * @param array $dataQuote [传参]源数据
	 * @param mixed $setData
	 */
	protected function setDataToEchoSite( array &$dataQuote, $setData ):void {
		com\Lib\Fun\Array_::assignPathWrite( $dataQuote, $this->getEchoSite(), $setData );
	}


	/**
	 * 获取"是否清理不完整的数据"
	 * @return bool
	 */
	public function getIsCleanIncompleteData():bool {
		if( $this->_option['isCleanIncompleteData'] !== null ) return $this->_option['isCleanIncompleteData'];
		//
		return $this->getDataUnit()->goOption()->getIsCleanIncompleteData();
	}

	/**
	 * 设置"是否清理不完整的数据"
	 * @param bool $isCleanIncompleteData
	 * @return $this
	 */
	public function setIsCleanIncompleteData( bool $isCleanIncompleteData = true ):self {
		$this->_option['isCleanIncompleteData'] = $isCleanIncompleteData;
		//
		return $this;
	}

}
